/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.statistics.service;

import java.util.List;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.z100h.supplychain.statistics.entity.ResourceLogEvent;
import com.z100h.supplychain.statistics.entity.SkuRealtimeInventory;
import com.z100h.supplychain.statistics.entity.WarehouseTypeRealtimeInventory;
import com.z100h.supplychain.statistics.mapper.StatisticsMapper;

/**
 * 供应链产品管理Service
 * @author madara
 * @version 2018-09-20
 */
@Service
@Transactional(readOnly = true)
public class StatisticsService {

	@Autowired
	private StatisticsMapper statisticsMapper;

	public List<SkuRealtimeInventory> statSkuRealtimeInventory() {
		return statisticsMapper.statSkuRealtimeInventory("1");
	}

	public List<WarehouseTypeRealtimeInventory> statWarehouseTypeRealtimeInventory() {

		List<WarehouseTypeRealtimeInventory> list = statisticsMapper.statWarehouseTypeRealtimeInventory("1");

		if (CollectionUtils.isNotEmpty(list)) {
			Integer total = 0;

			for (WarehouseTypeRealtimeInventory in : list) {
				total += in.getAmount();
			}
			Integer totalPer = 0;
			for (Integer i = 0; i < list.size(); i++) {
				WarehouseTypeRealtimeInventory in = list.get(i);
				if (i.equals(list.size() - 1)) {
					in.setPercentage(100 - totalPer);
				} else {
					Integer per = in.getAmount() / total * 100;
					totalPer += per;
					in.setPercentage(per);

				}
			}

		}
		return list;
	}

	public List<ResourceLogEvent> getResourceLogById(String resourceId) {
		return statisticsMapper.getResourceLogById(resourceId);
	}

	public String getIp(String label) {
		return statisticsMapper.getIp(label);
	}

}